package com.tju.resource.business.tb_bom;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tju.resource.business.tb_order.TbOrderService;
import com.tju.resource.mapper.TbBomMapper;
import com.tju.resource.mapper.TbOrderMapper;
import com.tju.resource.model.TbBom;
import com.tju.resource.model.TbOrder;
import com.tju.resource.result.Result;
import com.tju.resource.result.ResultEnum;
import com.tju.resource.result.ResultUtil;
import com.tju.resource.tool.tool.EmptyUtils;
import com.tju.resource.tool.tool.QuickQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *
 */
@Service
public class TbBomServiceImpl extends ServiceImpl<TbBomMapper, TbBom>
        implements TbBomService {

    @Autowired
    TbBomMapper mapper;

    @Autowired
    TbOrderMapper orderMapper;

    @Autowired
    TbOrderService orderService;

    @Override
    public Result<List<TbBom>> getAll(JSONObject params) {
        HashMap<String, Object> pageMap = new HashMap<>(params);

        try {
            Page<TbBom> pg = new Page<>(params.getInt("page"), params.getInt("limit"));
            IPage<TbBom> page = mapper.getAll(pg, pageMap);
            Result<List<TbBom>> bomRes = new Result<>();
            List<TbBom> records = page.getRecords();
            List<String> nos = new ArrayList<>();
            List<TbOrder> details;

            bomRes.setData(new ArrayList<>());
            bomRes.setCode(200);
            bomRes.setMsg("done.");
            bomRes.setCount(page.getTotal());

            records.forEach(rc -> nos.add(rc.getOrderNumber()));
            if (nos.size() > 0) {
                details = orderService.byNos(pageMap, nos);

                if (EmptyUtils.isNotEmpty(details)) {
                    details.forEach(tbOrder -> records.forEach(rc -> {
                        if (StringUtils.equals(tbOrder.getOrderNumber(), rc.getOrderNumber())) {
                            rc.setOrder(tbOrder);
                        }
                    }));
                    bomRes.setData(records);
                }
            }

            return bomRes;
        } catch (Exception e) {
            Result<List<TbBom>> r = new Result<>();

            r.setMsg(e.toString());
            r.setCode(500);
            e.printStackTrace();

            return r;
        }

    }

    @Override
    public Result<TbBom> byNo(JSONObject params) {
        HashMap<String, Object> pageMap = new HashMap<>(params);

        try {
            Page<TbBom> pg = new Page<>(params.getInt("page"), params.getInt("limit"));
            IPage<TbBom> page = mapper.getAll(pg, pageMap);
            Result<TbBom> bomRes = new Result<>();
            List<TbBom> records = page.getRecords();
            List<String> nos = new ArrayList<>();
            List<TbOrder> details;

            bomRes.setCode(200);
            bomRes.setMsg("done.");
            bomRes.setCount(page.getTotal());

            records.forEach(rc -> nos.add(rc.getOrderNumber()));
            if (nos.size() != 0) {
                details = orderService.byNos(pageMap, nos);

                if (EmptyUtils.isEmpty(details)) {
                    details.forEach(tbOrder -> records.forEach(rc -> {
                        if (StringUtils.equals(tbOrder.getOrderNumber(), rc.getOrderNumber())) {
                            rc.setOrder(tbOrder);
                        }
                    }));
                    bomRes.setData(records.get(0));
                }
            }

            return bomRes;
        } catch (Exception e) {
            Result<TbBom> r = new Result<>();

            r.setMsg(e.toString());
            r.setCode(500);
            e.printStackTrace();

            return r;
        }
    }

    @Override
    public Result<Object> upd(TbBom bom) {
        int updated;
        TbOrder order;
        TbBom oldBom = mapper.selectById(bom);

        if (null == oldBom) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "无相关BOM信息!");
        }

        if (null == bom.getOrderId()) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "BOM订单为空!");
        }
        order = QuickQuery.byOne(orderMapper, "id", bom.getOrderId());

        if (null == order) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "订单信息为空!");
        }

        bom.setUpdateTime(new Date());
        updated = mapper.updateById(bom);
        return updated > 0 ? ResultUtil.success(ResultEnum.SUCCESS, "BOM修改完成!") : ResultUtil.error(ResultEnum.UNKONW_ERROR, "BOM修改失败!");
    }

    @Override
    public Result<Object> add(TbBom bom) {
        int inserted;
        TbOrder order;

        if (null == bom.getOrderId()) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "BOM订单不可为空!");
        }
        order = orderMapper.selectById(bom.getOrderId());

        if (null != order) {
            bom.setBomNo("BOM-" + order.getOrderNumber());
        } else {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "订单信息不存在!");
        }

        inserted = mapper.insert(bom);
        return inserted > 0 ? ResultUtil.success(ResultEnum.SUCCESS, "新增BOM完成!") : ResultUtil.error(ResultEnum.UNKONW_ERROR, "BOM新增失败!");
    }

}




